package com.qidian.buy.item.mq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
*@author 戴着假发的程序员
*@company 起点编程
*@date 2023/9/15 11:40
*/
@Component // 将我们的监听者交给spring
public class MyReceive implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        // 获取消息的唯一性ID
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            String msg = message.toString();
            System.out.println("消费消息：" + msg);
            System.out.println("来自" + message.getMessageProperties().getConsumerQueue());
            if (1 == 1)
                throw new RuntimeException("主动出现一个异常");
            // 手动确认
            channel.basicAck(deliveryTag,true);
        } catch (IOException e) {
            // 拒绝策略
            channel.basicReject(deliveryTag, false);
            throw new RuntimeException(e);
        }
    }
}